package com.jwb.pub.serviceImpl;

import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.model.JavaModel;
import com.jwb.pub.model.Page;
import com.jwb.pub.service.AttachFileDetailService;
import com.jwb.util.ToolsUtil;


@Service
public class AttachFileDetailServiceImpl implements AttachFileDetailService {
	
	
	@Autowired
	private BaseDAO baseDAO;
	 /**
     * 文件上传
     * @param operCode
     * @param modeName
     * @param fileNo
     * @param fileId
     * @param filePath
     * @param fileName
     * @param fileType
     * @param fileSize
     * @param fileExt
     * @return
     */
	public JSONObject uploadSingle( String operCode,String modeName,
			String fileNo,String fileId,  String filePath, String fileName,
			String fileType, String fileSize, String fileExt) {
		boolean flg = false;
		JSONObject jsobject = new JSONObject();
		JSONArray jsarry = new JSONArray();
        String sql="insert into attach_file_detail"+
					"  (file_id,"+
					"   file_path,"+
					"   status,"+				
					"   oper_code,"+
					"   file_name,"+
					"   file_no,"+
					"   file_type,"+
					"   file_size,"+
					"   mod_name,"+
					"   file_ext)"+
				"	values"+
				"	  ('"+fileId+"',"+
				"	   '"+filePath+"',"+
				"	   'A',"+			
				"	   '"+operCode+"',"+
				"	  '"+fileName+"',"+
				"	   '"+fileNo+"',"+
				"	   '"+fileType+"',"+
				"	   '"+fileSize+"',"+
				"	   '"+modeName+"',"+
				"	   '"+fileExt+"')";	    
       
        
        
        flg=baseDAO.excuteSql(sql,null);
        if(flg)
        {           JSONObject jso = new JSONObject();         
			        jso.put("operCode", operCode);
					jso.put("modeName", modeName);
					jso.put("fileNo", fileNo);
					jso.put("fileId", fileId);
					jso.put("filePath", filePath);
					jso.put("fileName", fileName);
					jso.put("fileType", fileType);
					jso.put("fileSize", fileSize);
					jso.put("fileExt", fileExt);
					jsarry.add(0, jso);
					jsobject.put("fileList", jsarry);
					jsobject.put("respCode", "0");
					jsobject.put("respDesc", "文件上传成功");
   
        	
        }
        else
        {
        	jsobject.put("respCode", "-1");
        	jsobject.put("respDesc", "文件入库异常");
        }
        return jsobject;
		
	}

	
	
	public JSONObject uploadFileList(JSONObject jsdata, Page page) {
		JSONObject jsobject = new JSONObject();
		try
      	{      		 
      		 String sqlWhere="";
      		 String sqlOrder="";
      		String  sql = "select file_id, "+
					"      file_path, "+
					"       status, "+
					"       DATE_FORMAT(upload_date, '%Y-%m-%d') upload_date,"+
					"       oper_code, "+
					"       file_name, "+
					"       file_no, "+
					"       file_type, "+
					"       file_size, "+
					"       mod_name, "+
					"       file_ext, "+					
					"       dft_show "+
					"  from attach_file_detail where 1=1 and status='A' ";
				 String fileId="";
				 if (jsdata.containsKey("fileId"))
				 {
					 fileId=jsdata.getString("fileId");
					 
				 }
				 if (!"".equals(fileId))
				 {
					sqlWhere += " and file_id = '"+fileId+"'";
				 }
				 
				 String fileNo="";
				 if (jsdata.containsKey("fileNo"))
				 {
					 fileNo=jsdata.getString("fileNo");
					 
				 }
				 
				 if (!"".equals(fileNo))
				 {
					sqlWhere += " and file_no = '"+fileNo+"'";
				 } 
				 
				 String modeName="";
				 if (jsdata.containsKey("modeName"))
				 {
					 modeName=jsdata.getString("modeName");
					 
				 }
				 
				 if (!"".equals(modeName))
				 {
					sqlWhere += " and modeName = '"+modeName+"'";
				 }  
				 
				 String operCode="";
				 if (jsdata.containsKey("operCode"))
				 {
					 operCode=jsdata.getString("operCode");
					 
				 }
				 
				 if (!"".equals(operCode))
				{
					sqlWhere += " and oper_code = '"+operCode+"'";
				}  
				 
				 String startDate="";
				 if (jsdata.containsKey("startDate"))
				 {
					 startDate=jsdata.getString("startDate");
					 
				 }
				 String endDate="";
				 if (jsdata.containsKey("endDate"))
				 {
					 endDate=jsdata.getString("endDate");
					 
				 }
				 
				 if (!"".equals(startDate) )
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(upload_date,'%Y-%m-%d'),'%Y-%m-%d') >= "
							+ "STR_TO_DATE('" + startDate + " ','%Y-%m-%d')";
				}
				if (!"".equals(endDate))
				{
					sqlWhere += " and STR_TO_DATE(DATE_FORMAT(upload_date,'%Y-%m-%d'),'%Y-%m-%d') <= "
							+ "STR_TO_DATE('" + endDate + " ','%Y-%m-%d')";
				}
				 
				
				 String filePath="";
				 if (jsdata.containsKey("filePath"))
				 {
					 filePath=jsdata.getString("filePath");
					 
				 }
				 
				 if (!"".equals(filePath))
				{
					//	sqlWhere += " and substr(file_path,instr(file_path,'/',0),instr(file_path,'/',-1)-1) in ("+filePath+") ";
				}  
			    sqlOrder=" ";
			    sql+=sqlWhere+sqlOrder;	       
			    jsobject=baseDAO.findBySqlToJosn(sql,page);
      		
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}
	
	/**
	 * 附件删除
	 * @param sqlWhere
	 * @return
	 */
	public JSONObject fileDelete(String sqlWhere,String filePath)
	{
		boolean flg = false;
		JSONObject jsobject = new JSONObject();
		JSONArray jsarry = new JSONArray();
        String sql="update attach_file_detail set "+
							" status = 'P'   "+    
							" where 1=1 "+ sqlWhere;;	
        flg=baseDAO.excuteSql(sql, null);
        if(flg)
        {         JSONObject jso = new JSONObject();         
                  File f = new File(filePath);				
		          if(!f.exists()||!f.isFile()) 
		          {
		        	  System.out.println("文件不存在,无法删除"+filePath);
		          }
		          else
		          {
		        	  f.delete();
		        	  System.out.println("文件存在,删除成功"+filePath);
		        	
		          }
			
				   jsobject.put("respCode", "0");
				  jsobject.put("respDesc", "文件删除成功");
   
        	
        }
        else
        {
        	jsobject.put("respCode", "-1");
        	jsobject.put("respDesc", "文件删除失败");
        }
        return jsobject;
	}



	@Override
	public JSONObject fileInfoSave(JSONObject jsdata) {
		   JSONObject jsobject = new JSONObject();	
		   jsobject.put("respCode", "-1");
		   jsobject.put("respDesc", "数据获取异常");
		   String file_id=!jsdata.containsKey("file_id") ? "" : jsdata.getString("file_id");	
		   String file_path=!jsdata.containsKey("file_path") ? "" : jsdata.getString("file_path");	
		   String domain_name=!jsdata.containsKey("domain_name") ? "" : jsdata.getString("domain_name");	
		   String source=!jsdata.containsKey("source") ? "mp" : jsdata.getString("source");
		   String info=!jsdata.containsKey("info") ? "" : jsdata.getString("info");
		   String url=!jsdata.containsKey("url") ? "" : jsdata.getString("url");
		   String convert_url=!jsdata.containsKey("convert_url") ? "" : jsdata.getString("convert_url");
		   String status=!jsdata.containsKey("status") ? "A" : jsdata.getString("status");
		   String file_type=!jsdata.containsKey("file_type") ? "" : jsdata.getString("file_type");
		   String place=!jsdata.containsKey("place") ? "" : jsdata.getString("place");
		   String sql="";
		   ArrayList<JavaModel> list = new ArrayList<JavaModel>();
		   if("".equals(file_id))
		   {
				   sql="INSERT INTO file_info(file_id, file_path,domain_name,source,info,url,file_type,convert_url,status,place) "+
				       "  VALUES (?,?,?,?,?,?,?,?,?,?) ";				  
				   file_id=ToolsUtil.get32bituuid();
				   list.add(new JavaModel(file_id)); 	
				   list.add(new JavaModel(file_path)); 	
				   list.add(new JavaModel(domain_name)); 	
				   list.add(new JavaModel(source)); 		  	
				   Reader sqlClob = new StringReader(info);		
			       list.add(new JavaModel(sqlClob));
			       list.add(new JavaModel(url)); 
			       list.add(new JavaModel(file_type)); 
			       list.add(new JavaModel(convert_url)); 
			       list.add(new JavaModel(status)); 
			       list.add(new JavaModel(place)); 
			       System.out.println("fileInfoSave--add--file_id="+file_id);
		   }	
		   else
		   {
			   sql=" UPDATE  "+
					"   file_info  "+
					"   SET  "+
					"     file_path = ?, "+
					"     status = ?, "+
					"     upload_date =NOW(),"+
					"     domain_name = ?,"+
					"     source = ?,"+
					"     info = ?,"+
					"     url = ?,"+
					"     file_type = ?,"+
					"     convert_url = ? "+
					"   WHERE file_id = ?" ; 
			   list.add(new JavaModel(file_path)); 	
			   list.add(new JavaModel(status)); 
			   list.add(new JavaModel(domain_name)); 
			   list.add(new JavaModel(source)); 	
			   Reader sqlClob = new StringReader(info);		
		       list.add(new JavaModel(sqlClob));
		       list.add(new JavaModel(url));
		       list.add(new JavaModel(file_type)); 
		       list.add(new JavaModel(convert_url)); 
			   list.add(new JavaModel(file_id)); 	    
			   System.out.println("fileInfoSave--edit--file_id="+file_id);
		   }
	       
	       
	       boolean flag=baseDAO.excuteSql(sql, list);
	        if(flag)
	        {
	        	jsobject.put("respCode", "0");
				jsobject.put("respDesc", "数据保存成功!");	
	        }
	        else
	        {
	        	jsobject.put("respCode", "-1");
				jsobject.put("respDesc", "数据保存失败!");	
	        }
			
			return jsobject;
	}



	@Override
	public JSONObject filelist(JSONObject jsdata, Page page) {
		JSONObject jsobject = new JSONObject();
		 String pageFlag=!jsdata.containsKey("pageFlag") ? "" : jsdata.getString("page_flag");
		 String file_type=!jsdata.containsKey("file_type") ? "" : jsdata.getString("file_type");
		 String status=!jsdata.containsKey("status") ? "A" : jsdata.getString("status");
		 String source=!jsdata.containsKey("source") ? "" : jsdata.getString("source");
		 String place=!jsdata.containsKey("place") ? "" : jsdata.getString("place");
		 String file_id=!jsdata.containsKey("file_id") ? "" : jsdata.getString("file_id");
		 try
      	{      		 
      		    String sqlWhere="";
      		    String sqlOrder="";
      		    if(!"".equals(status))
      		    {
      		    	sqlWhere+=" and STATUS='"+status+"'";
      		    }
      		    
      		    if(!"".equals(file_type))
    		    {
    		    	sqlWhere+=" and file_type='"+file_type+"'";
    		    }
      		    
      		    if(!"".equals(source))
    		    {
    		    	sqlWhere+=" and source='"+source+"'";
    		    }
      		    
      		    if(!"".equals(place))
	  		    {
	  		    	sqlWhere+=" and place='"+place+"'";
	  		    }
      		   if(!"".equals(file_id))
	  		    {
	  		    	sqlWhere+=" and file_id='"+file_id+"'";
	  		    }
      		    String  sql = "SELECT file_id,  file_path,  status, DATE_FORMAT(upload_date, '%Y-%m-%d %H:%i:%s')  upload_date,  domain_name,  source,  info,  url,  file_type,  convert_url ,place FROM file_info WHERE 1=1 and status='A' ";				 
			    sqlOrder=" ORDER BY upload_date DESC";
			    sql+=sqlWhere+sqlOrder;	  
			    if("".equals(pageFlag))
			    {
			    	jsobject=baseDAO.findBySqlToJosn(sql,page);
			    }
			    else
			    {
			    	jsobject=baseDAO.findBySqlToJosn(sql);
			    }
      	}   
      	catch(Exception e)
      	{
      		
      	}
      	
		return jsobject;
	}	
	
	
	
	/**
	 * 附件删除
	 * @param sqlWhere
	 * @return
	 */
	public JSONObject fileDel(String sqlWhere,String filePath)
	{
		boolean flg = false;
		JSONObject jsobject = new JSONObject();
		JSONArray jsarry = new JSONArray();
        String sql="delete from  file_info "+						
							" where 1=1 "+ sqlWhere;;	
        flg=baseDAO.excuteSql(sql, null);
        if(flg)
        {         JSONObject jso = new JSONObject();         
                  File f = new File(filePath);				
		          if(!f.exists()||!f.isFile()) 
		          {
		        	  System.out.println("文件不存在,无法删除"+filePath);
		          }
		          else
		          {
		        	  f.delete();
		        	  System.out.println("文件存在,删除成功"+filePath);
		        	
		          }
			
				   jsobject.put("respCode", "0");
				  jsobject.put("respDesc", "文件删除成功");
   
        	
        }
        else
        {
        	jsobject.put("respCode", "-1");
        	jsobject.put("respDesc", "文件删除失败");
        }
        return jsobject;
	}
	
	public JSONObject addname(JSONObject jsdata) {
		   JSONObject jsobject = new JSONObject();	
		   jsobject.put("respCode", "-1");
		   jsobject.put("respDesc", "数据获取异常");
		   String sql="";
		   try
	      	{ 
			   String name=!jsdata.containsKey("name") ? "" : jsdata.getString("name");	

			   ArrayList<JavaModel> list = new ArrayList<JavaModel>();
			   sql=" UPDATE  "+
						"   file_info  "+
						"   SET  "+				
						"     place = ? "+	
						"   WHERE place ='' OR place IS NULL " ; 
				   list.add(new JavaModel(name)); 	
				   boolean flag=baseDAO.excuteSql(sql, list);
			        if(flag)
			        {
			        	jsobject.put("respCode", "0");
						jsobject.put("respDesc", "添加标签成功!");	
			        }
			        else
			        {
			        	jsobject.put("respCode", "-1");
						jsobject.put("respDesc", "添加标签失败!");	
			        }
					    
	      	}
		   catch(Exception e)
	      	{
	      		
	      	}
	      	
		   return jsobject;
	}
}
